package com.a16.yuejian.yuejian.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.SerachActivity;
import com.a16.yuejian.yuejian.adapter.ActivityAdapter;
import com.a16.yuejian.yuejian.adapter.GameAdapter;
import com.a16.yuejian.yuejian.adapter.MasterAdapter;
import com.a16.yuejian.yuejian.javabean.Activity;
import com.a16.yuejian.yuejian.javabean.Game;
import com.a16.yuejian.yuejian.javabean.Master;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.model.ActivityModel;
import com.a16.yuejian.yuejian.model.GameModel;
import com.a16.yuejian.yuejian.model.MasterModel;
import com.oragee.banners.BannerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class HotFragment extends Fragment implements View.OnClickListener{
    private String[] imgs = {"1.jpg","2.jpg","3.jpg","4.jpg"};
    private List<View> viewList;
    BannerView bannerView;
    private TextView activity,master,game;
    private View view=null;
    private ImageView serach;
    private RecyclerView recyclerview;


    Listener<Activity> Alistener = new Listener<Activity>() {
        @Override
        public void onResponse(List<Activity> t) {
            ActivityAdapter activityAdapter = new ActivityAdapter(getActivity());
            activityAdapter.setList(t);
            recyclerview.setAdapter(activityAdapter);
        }

        @Override
        public void onFail() {

        }
    };

    Listener<Master> Mlistener = new Listener<Master>() {
        @Override
        public void onResponse(List<Master> t) {
            MasterAdapter masterAdapter = new MasterAdapter(getActivity());
            masterAdapter.setList(t);
            recyclerview.setAdapter(masterAdapter);
        }

        @Override
        public void onFail() {

        }
    };

    Listener<Game> Glistener = new Listener<Game>() {
        @Override
        public void onResponse(List<Game> t) {
            GameAdapter gameAdapter = new GameAdapter(getActivity());
            gameAdapter.setList(t);
            recyclerview.setAdapter(gameAdapter);
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.fragment_hot, container, false);
        initRecyclerview(view);
        ActivityModel activityModel = new ActivityModel();
        activityModel.allactivity(Alistener);
        initViews();
        initEvents();
        viewList = new ArrayList<View>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getActivity());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            Picasso.with(getContext()).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+imgs[i]).into(image);
            viewList.add(image);
        }
        bannerView.startLoop(true);
        bannerView.setViewList(viewList);
        return view;
    }

    private void initEvents() {
        activity.setOnClickListener(this);
        master.setOnClickListener(this);
        game.setOnClickListener(this);
        serach.setOnClickListener(this);
    }

    private void initViews() {
        activity = (TextView)view.findViewById(R.id.hot_activity);
        master = (TextView)view.findViewById(R.id.hot_master);
        game = (TextView)view.findViewById(R.id.hot_game);
        bannerView = (BannerView)view.findViewById(R.id.banner);
        serach = (ImageView)view.findViewById(R.id.hot_serach);
    }

    private void initRecyclerview(View view) {
        recyclerview = (RecyclerView)view.findViewById(R.id.rv_hot);
        recyclerview.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.hot_activity:
                activity.setTextColor(Color.parseColor("#FCCA00"));
                master.setTextColor(Color.parseColor("#C8C8C8"));
                game.setTextColor(Color.parseColor("#C8C8C8"));
                ActivityModel activityModel = new ActivityModel();
                activityModel.allactivity(Alistener);
                break;

            case R.id.hot_master:
                activity.setTextColor(Color.parseColor("#C8C8C8"));
                master.setTextColor(Color.parseColor("#FCCA00"));
                game.setTextColor(Color.parseColor("#C8C8C8"));
                MasterModel masterModel = new MasterModel();
                masterModel.allmaster(Mlistener);
                break;

            case R.id.hot_game:
                activity.setTextColor(Color.parseColor("#C8C8C8"));
                master.setTextColor(Color.parseColor("#C8C8C8"));
                game.setTextColor(Color.parseColor("#FCCA00"));
                GameModel gameModel = new GameModel();
                gameModel.allgame(Glistener);
                break;
            case R.id.hot_serach:
                Intent intent = new Intent(getActivity(), SerachActivity.class);
                startActivity(intent);
                break;
        }
    }
}
